package com.company.creational.prototype_pattern.deep_clone;

import java.io.*;

/**
 * @ClassName WeeklyLog
 * @Author tiezheng.han
 * @Date 2020/8/27 15:56
 **/
public class WeeklyLog implements Serializable {

    /**
     *  假设一份周报中有一份附件
     */
    private Attachment attachment;

    private String name;

    private String date;

    private String content;

    public Attachment getAttachment() {
        return attachment;
    }

    public void setAttachment(Attachment attachment) {
        this.attachment = attachment;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 使用 clone()方法实现浅克隆
     */
    public WeeklyLog deepClone() throws IOException, ClassNotFoundException {
        // 将对象写入流中
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);
        // 将对象从流中取出
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (WeeklyLog)ois.readObject();
    }
}
